package com.ihr360.payroll.controller.salaryfield;


import com.alibaba.fastjson.JSONObject;
import com.ihr360.commons.model.type.BenefitsPayer;
import com.ihr360.commons.model.type.FieldSource;
import com.ihr360.commons.model.type.NumOperation;
import com.ihr360.commons.model.type.RoundingMode;
import com.ihr360.commons.model.type.SalaryFieldValueType;
import com.ihr360.commons.vo.PageData;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.config.system.SystemSalaryFieldEntity;
import com.ihr360.payroll.model.dto.salary.meta.system.SystemSalaryField;
import com.ihr360.payroll.request.SystemSalaryFieldRequest;
import com.ihr360.payroll.service.salaryfield.SystemSalaryFieldService;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author richey
 */
@RestController
@RequestMapping("/systemSalaryFields/")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE_ADMIN, mainModuleName = ModuleCodeStore.Module.SALARY_NAME_ADMIN, hasExtendsAbstractRestController = true,
        subModuleCode = ModuleCodeStore.Module.SalaryAdmin.SYSTEM_SALARY_FIEL_CODE, subModuleName = ModuleCodeStore.Module.SalaryAdmin.SYSTEM_SALARY_FIEL_NAME)
public class SystemSalaryFieldController extends AbstractRestSimpleWithLongIdController<SystemSalaryField> {

    @Autowired
    private SystemSalaryFieldService systemSalaryFieldService;

    @Override
    public SimpleRestService<SystemSalaryField, Long> getSimpleRestService() {
        return systemSalaryFieldService;
    }

    @ApiOperation(value = "枚举集合")
    @GetMapping(value = "/enumerations")
    @FunctionDefinition(functionName = "枚举集合", functionCode = ModuleCodeStore.Function.VIEW)
    public JSONObject getEnumerations() {
        JSONObject result = new JSONObject();
        result.put("salaryFieldValueTypes", SalaryFieldValueType.values());
        result.put("roundingModes", RoundingMode.values());
        result.put("fieldSources", FieldSource.values());
        result.put("numOperations", NumOperation.getAddSub());
        result.put("benefitsPayers", BenefitsPayer.values());
        return result;
    }


    @ApiOperation(value = "根据类型获取元数据分页集合")
    @PostMapping(value = "/pageList/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "根据类型获取薪资元数据分页集合", functionCode = ModuleCodeStore.Function.VIEW)
    public PageData<SystemSalaryFieldEntity> pageListByCategory(@RequestBody SystemSalaryFieldRequest param) {
        Pageable pageable = param.getPageable();
        return systemSalaryFieldService.pageListByCategory(param.getCategoryId(), pageable);
    }

    @ApiOperation(value = "根据类型获取元数据集合")
    @GetMapping(value = "/list/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "根据类型获取元数据集合", functionCode = ModuleCodeStore.Function.VIEW)
    public List<SystemSalaryFieldEntity> listByCategory(@RequestParam("categoryId") Long categoryId) {
        return systemSalaryFieldService.listByCategory(categoryId);
    }

    @ApiOperation(value = "根据类型获取元数据")
    @GetMapping(value = "/list/salaryFieldType", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "根据类型获取元数据集合", functionCode = ModuleCodeStore.Function.VIEW)
    public List<SystemSalaryFieldEntity> listBySalaryFieldType(@RequestParam("salaryFieldTypeId") Long salaryFieldTypeId, @RequestParam(value = "showInStaffSalary", required = false) Boolean showInStaffSalary) {
        return systemSalaryFieldService.listBySalaryFieldType(salaryFieldTypeId, showInStaffSalary);
    }




    @ApiOperation(value = "获取除指定id外的元数据集合")
    @PostMapping(value = "/list/excludeId", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "获取除指定id外的元数据集合", functionCode = ModuleCodeStore.Function.VIEW)
    public List<SystemSalaryFieldEntity> listByType(@RequestBody SystemSalaryFieldRequest param) {
        return systemSalaryFieldService.listExcludeId(param.getExcludeId());
    }

    //todo add or update 权限
    @ApiOperation(value = "保存或更新薪酬元数据")
    @PostMapping(value = {"/save", "/update"}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "保存或更新薪酬元数据", functionCode = ModuleCodeStore.Function.UPDATE)
    public SystemSalaryFieldEntity saveOrUpdate(@RequestBody SystemSalaryFieldEntity param) {
        return systemSalaryFieldService.saveOrUpdate(param);
    }

    @ApiOperation(value = "获取薪资字段编码获取薪资字段")
    @GetMapping(value = {"/fieldCode"}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DisableResponseAdvice
    @FunctionDefinition(functionName = "获取薪资字段编码获取薪资字段", functionCode = ModuleCodeStore.Function.VIEW)
    public SystemSalaryFieldEntity getSalaryField(@RequestParam("fieldCode") String fieldCode) {
        return systemSalaryFieldService.findByCode(fieldCode);
    }


    @GetMapping(value = {"/search/{id}"}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @FunctionDefinition(functionName = "根据Id获取系统薪资元数据", functionCode = ModuleCodeStore.Function.VIEW)
    public SystemSalaryFieldEntity getById(@PathVariable Long id) {
        return systemSalaryFieldService.findById(id);
    }

    @GetMapping(value = "/countField")
    @FunctionDefinition(functionName = "根据统计项过滤查询系统薪资字段", functionCode = ModuleCodeStore.Function.VIEW)
    public List<SystemSalaryFieldEntity> filterCountField(@RequestParam("countField") boolean countField) {
        return systemSalaryFieldService.filterCountField(countField);
    }

    @GetMapping(value = "/totalField")
    @FunctionDefinition(functionName = "获取所有统计项类型的系统薪资字段", functionCode = ModuleCodeStore.Function.VIEW)
    public List<SystemSalaryFieldEntity> getAllSystemSalaryCountField() {
        return systemSalaryFieldService.getAllStatisticsSystemSalaryField();
    }


}
